# This code is sourced by cv_ALL or ev_ALL scripts
# in order to set up certain variables in a standard way.
# The variables are then used by the sourcing script in its
# subsequent code. The variables are generally used to point to
# the different types of data, and to encapusalate the commands
# used etc.



#******************************************************************************
# compile ()
#
# This function is used when we want to do a normal leapfrog compile (cv).
# It strips out vhdlcover related arguments, passing the remainder
# to the cv command (given in COMPILE_COMMAND).
#******************************************************************************
compile ()
{
    skip_arg="no"
    cv_args=""

    while [ "$1" ]  ; do

	case "$1" in

	    # Get the vcanalyse arguments that will require a value
	    ( -s[it]* | -[dglqr]* | -ne* | -instrument_* | -cont* | ^-com* )
		skip_arg="yes"
	    ;;

	    # Get the vcanalyse arguments that are switches
	    ( -* )
		skip_arg="no"
	    ;;

	    # The rest
	    ( * )
		if [ "$skip_arg" = "no" ] ; then
		    cv_args="$cv_args $1"
		else
		     skip_arg="no"                
		fi
	    ;;

	esac

	shift

    done 

#echo "We have: $cv_args"

    $COMPILE_COMMAND $cv_args

}


#******************************************************************************
# Set the architecture.
if [ `uname -s` = "HP-UX" ] ; then
    arch=hp ;
else
    arch=sun4v ;
fi

# Now set up a standard set of variables, assuming WORKLIB and VERSION 
# have been set to the name of the library by the calling script.
if [ "$WORKLIB" = "" -o "$TYPE" = "" ] ; then
    echo "Error: Script did not set WORKLIB or TYPE variable."
    exit 1
fi

# For backwards compatability, process the TYPE variable
# Note that we use the variable VCANALYSE to detect if we are actually
# running a vcanalyse rather than a normal cv run.
if [ "$TYPE" = "cv" ] ; then
    TYPE=cv_ALL
fi   
if [ "$TYPE" = "ev" ] ; then
    TYPE=ev_ALL
fi   
if [ "$TYPE" = "vcanalyse" ] ; then
    TYPE=vcanalyse_ALL
fi

if [ "$VCANALYSE" ] ; then
    TYPE=`echo $TYPE | sed -e 's/cv_/vcanalyse_/1'`
fi

# We use the getLibFullPath program to locate the full path to the
# library
LIBDIR=`/projects/tsunami/project_technology/dtmf/logic/bin/getLibFullPath $WORKLIB`

if [ $? -ne 0 ] ; then
    echo "Aborting..."
    exit 1
fi

SOURCEDIR=$LIBDIR/logic/src


# If we are doing a vcanalyse, then set the locations to point into
# the vhdlcover area. We will also want the COMPILE and ENTTOPACK
# commands set differently.
case $TYPE in
    ( vcanalyse* )
	COVERDATADIR=$LIBDIR/logic/lib/vhdlcover.src
	OBJECTDIR=$LIBDIR/logic/lib/vhdlcover.$arch
	LOGFILE=$LIBDIR/logic/lib/log/${TYPE}.$arch.log
	CDSLIBFILE=$LIBDIR/../../proj_env/cds.logic/lib.vc.$arch
	ANALYSE="vcanalyse -data_dir $COVERDATADIR"
#	ANALYSE="vcanalyse"
	COMPILE="$ANALYSE"
	ENTTOPACK=":"

        # Make sure the log directory exists
        if [ ! -d $LIBDIR/logic/lib/log ] ; then
	    mkdir $LIBDIR/logic/lib/log
            mkdir $LIBDIR/logic/lib/vhdlcover.src
        fi

	# For vcanalyse, we really have to be running
	# in the right place, i.e. just inside the vhdlcover
	# directory. We must have a regular cds.lib & hdl.var
	# in this location.
	cd $LIBDIR/logic/work/vhdlcover
	if [ ! -e cds.lib -o ! -e hdl.var ] ; then
	    echo "ERROR: For vcanalyse you must have a regular cds.lib & hdl.var"
	    echo "Aborting ..."
	    exit 1
	fi

    ;;

    ( * )
        OBJECTDIR=$LIBDIR/logic/lib/leapfrog.$arch
        LOGFILE=$LIBDIR/logic/lib/log/${TYPE}.$arch.log
echo $LOGFILE

        # Set to use the appropriate cds.lib file. For release versions
        # this is release_cds.lib.arch, for dev versions it is cds.lib.arch
        CDSLIBFILE=$LIBDIR/../proj_env/cds.lib.$arch

	COMPILE="compile"
	COMPILE_COMMAND="cv   -cdslib $CDSLIBFILE -work $WORKLIB -messages -compatibility -nocopyright"

        # Make sure the log directory exists
        if [ ! -d $LIBDIR/logic/lib/log ] ; then
	    mkdir $LIBDIR/logic/lib/log
        fi
    ;;

esac


# Set the other execution variables. These encapsulate commands that the calling
# script will use
ELABORATE="ev -cdslib $CDSLIBFILE -work $WORKLIB -messages -compatibility -nocopyright"
CLEAR="rm -rf $OBJECTDIR"
NEW="mkdir $OBJECTDIR"
CLEAR_LOG="rm -f $LOGFILE ; touch $LOGFILE"
START_LOG="tee $LOGFILE"
ADD_TO_LOG="tee -a $LOGFILE"
#ANALYSE="vcanalyse -data_dir $LIBDIR/$VERSION/src"

# Thats it!
